package com.binhthuan.catechist.client.content.manage;

import com.binhthuan.catechist.client.AbstractContent;
import com.binhthuan.catechist.client.ContentFactory;
import com.binhthuan.catechist.client.ECApplication;
import com.binhthuan.catechist.client.ECIcons;
import com.binhthuan.catechist.client.component.PopupDialog;
import com.binhthuan.catechist.client.data.YearDS;
import com.binhthuan.catechist.client.form.YearForm;
import com.binhthuan.catechist.client.grid.YearGrid;
import com.binhthuan.catechist.shared.Utilities;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Dialog;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.events.DataArrivedEvent;
import com.smartgwt.client.widgets.grid.events.DataArrivedHandler;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

public class ManageSchoolYear extends AbstractContent {

    public static class Factory implements ContentFactory {

        private static Factory instance;
        private String id;

        private Factory() {
            if (id == null) {
                id = Utilities.generateID(ManageSchoolYear.class.getName());
            }
        }

        /**
         * @return the instance
         */
        public static Factory getInstance() {
            if (instance == null) {
                instance = new Factory();
            }

            return instance;
        }

        @Override
        public boolean checkCertificated(long securityCode) {
            return true;
        }

        @Override
        public AbstractContent create() {
            ManageSchoolYear content = new ManageSchoolYear();
            return content;
        }

        @Override
        public String getID() {
            return id;
        }

        @Override
        public String getIcon() {
            return ECIcons.getIcon("year.png");
        }

        @Override
        public String getTitle() {
            return ECApplication.getConstants().schoolYear();
        }
    }

    private PopupDialog inputDialog;
    private TabSet detailTabset;
    private YearGrid reportGrid;
    private YearForm updateEditor;
    private Label unselectedLabel;
    private Label totalItems;

    @Override
    public Canvas getContentPanel() {
        VLayout mainLayout = new VLayout();
        mainLayout.setWidth100();
        mainLayout.setHeight100();

        SectionStack stack = new SectionStack();
        stack.setWidth100();
        stack.setHeight100();
        stack.setAnimateSections(true);
        stack.setShowEdges(true);
        stack.setVisibilityMode(VisibilityMode.MULTIPLE);

        totalItems = new Label();
        totalItems.setAlign(Alignment.RIGHT);

        ToolStripButton addButton = new ToolStripButton();
        addButton.setIcon(ECIcons.getIcon("add.png"));
        addButton.setPrompt(ECApplication.getMessages().addItem(ECApplication.getConstants().schoolYear()));
        addButton.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                ensureCreateDialog().show();
            }
        });

        ToolStripButton deleteButton = new ToolStripButton();
        deleteButton.setIcon(ECIcons.getIcon("delete.png"));
        deleteButton.setPrompt(ECApplication.getMessages().deleteItem(ECApplication.getConstants().schoolYear()));
        deleteButton.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                reportGrid.removeSelectedItems(true);
            }
        });

        ToolStrip toolStrip = new ToolStrip();
        toolStrip.addButton(addButton);
        toolStrip.addButton(deleteButton);

        reportGrid = new YearGrid(YearDS.getInstance());
        reportGrid.addDataArrivedHandler(new DataArrivedHandler() {

            @Override
            public void onDataArrived(DataArrivedEvent event) {
                totalItems.setContents(ECApplication.getMessages().numberOfResult(reportGrid.getTotalRows()));
                reportGrid.deselectAllRecords();
                updateDetailPanel();
            }
        });

        reportGrid.addRecordClickHandler(new RecordClickHandler() {

            @Override
            public void onRecordClick(RecordClickEvent event) {
                updateDetailPanel();
            }
        });

        SectionStackSection reportSection = new SectionStackSection(ECApplication.getConstants().schoolYear());
        reportSection.setCanCollapse(false);
        reportSection.setExpanded(true);
        reportSection.setItems(reportGrid);
        reportSection.setControls(totalItems, addButton, deleteButton);

        updateEditor = new YearForm(YearDS.getInstance());

        detailTabset = new TabSet();
        detailTabset.setWidth100();
        detailTabset.setHeight(165);

        unselectedLabel = new Label();
        unselectedLabel.setHeight100();
        unselectedLabel.setWidth100();
        unselectedLabel.setAlign(Alignment.CENTER);
        unselectedLabel.setContents(ECApplication.getMessages().noItemToDisplay());

        Tab editTab = new Tab(ECApplication.getConstants().edit());
        editTab.setIcon(ECIcons.getIcon("edit.png"));
        editTab.setWidth(100);
        editTab.setPane(unselectedLabel);

        detailTabset.setTabs(editTab);

        SectionStackSection detailSection = new SectionStackSection(ECApplication.getConstants().detail());
        detailSection.setExpanded(true);
        detailSection.addItem(detailTabset);

        stack.setSections(reportSection, detailSection);
        mainLayout.setMembers(stack);

        return mainLayout;
    }

    @Override
    protected String getHelpContent() {
        return "This is help content for manage school year page";
    }

    public Dialog ensureCreateDialog() {
        if (inputDialog == null) {
            inputDialog = new PopupDialog(ECApplication.getMessages().addItem(ECApplication.getConstants().schoolYear()));

            YearForm createEditor = new YearForm(YearDS.getInstance()) {

                @Override
                protected void onUpdateSuccess() {
                    editNewRecord();
                    inputDialog.closeClick();
                }
            };
            inputDialog.addItem(createEditor);
        }

        return inputDialog;
    }

    public void updateDetailPanel() {
        if (reportGrid.anySelected()) {
            updateEditor.clearErrors(false);
            updateEditor.editRecord(reportGrid.getSelectedRecord());

            detailTabset.updateTab(0, updateEditor);
        } else {
            detailTabset.updateTab(0, unselectedLabel);
        }
    }

    /* (non-Javadoc)
     * @see com.binhthuan.catechist.client.AbstractContent#getPrintPreviewPanel()
     */
    @Override
    protected Canvas getPrintPreviewPanel() {
        return reportGrid;
    }
}
